package com.hu.tool.service.importService.importServiceImpl;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.hu.tool.service.importService.ImportService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author linyh
 * @version 1.0
 * @email 1503386669@qq.com
 * @date 2020/10/20 17:24
 */
@Service
public class ImportServiceImpl implements ImportService {

    @Override
    public void readExcelFile(MultipartFile file) {
        // 从流中读取Excel为ExcelReader
        ExcelReader readers = null;
        try {
            readers = ExcelUtil.getReader(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 得到第一个shell
        Sheet sheet = readers.getSheet();
        // 得到Excel的行数
        int rows = sheet.getLastRowNum();
        // 得到Excel的列数
        int columns = sheet.getRow(0).getPhysicalNumberOfCells();
        // 遍历行(从1开始，去除Excel表头数据)
        for (int i = 1; i <= rows; i++) {
            Row row = sheet.getRow(i);
            // 在某行的基础下，遍历该行所有列k
            if(null != row){
                for (int i1 = 0; i1 < columns; i1++) {
                    // 获取单元格
                    Cell cell = row.getCell(i1);
                    if(null != cell){
                        if(i == rows && i1 == columns-2){
                            System.out.println(cell.getNumericCellValue());
                        }else{
                            System.out.print(cell.toString()+"\t");
                        }
                    }
                }
            }
            System.out.println();
        }
    }

}
